import { FModule } from "../../Libs/Framework/Support/FModule";
import { EventManager } from "../../Libs/Framework/Managers/EventManager";
import { ModuleEvent } from "../../Libs/Framework/Event/ModuleEvent";
import { ModuleNames } from "../../Libs/Framework/Enums/ModuleNames";

export default class ConfirmModule extends FModule {
    public get assets(): any { return ["Common/Confirm"] }


    private content: cc.Label = null
    private Close: cc.Node = null
    private Confirm: cc.Node = null


    //确定
    private _confirm: Function = null;
    //取消
    private _cancel:Function = null;

    protected createViews(): void {
        super.createViews()

        this.content = this.node.getChildByName("content").getComponent(cc.Label)
        this.Close = this.node.getChildByName("close")
        this.Confirm = this.node.getChildByName("confirm")

        if(typeof this.moduleData.Confirm == "function"){
            this._confirm = this.moduleData.Confirm
            this.Confirm.active = true
        }else{this.Confirm.active = false}

        if(typeof this.moduleData.Cancel == "function"){
            this._cancel = this.moduleData.Cancel
        this.Close.active = true
        }else{this.Close.active = false}

        this.content.string = this.moduleData.content
    }

    public addEvents(): void {
        if(this._confirm){this.Confirm.on(cc.Node.EventType.TOUCH_END,this.onConfirm,this)}
        if(this._cancel){this.Close.on(cc.Node.EventType.TOUCH_END,this.onClose,this)}
    }


    private onConfirm () : void {
        if(this._confirm){
            this._confirm()
        }
    }

    private onClose () : void {
        if(this._cancel){
            this._cancel()
        }
        EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Confirm);
    }


    public dispose(): void {
        super.dispose();
        this.Confirm.off(cc.Node.EventType.TOUCH_END,this.onConfirm)
        this.Close.off(cc.Node.EventType.TOUCH_END,this.onClose)
    }
}